package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SetmealDTO setmealDTO) {
        //新增套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmealMapper.save(setmeal);

        //新增套餐菜品关系表
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.save(setmealDish);
        }
        String key = "setmeat:"+setmealDTO.getCategoryId();
        redisTemplate.delete(key);


    }

    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<SetmealVO> list= setmealMapper.page(dto);
        Page<SetmealVO> list2= (Page<SetmealVO>) list;
        return new PageResult(list2.getTotal(),list2.getResult());
    }

    @Override
    public void remove(List<Integer> ids) {
        for (Integer id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        setmealMapper.remove(ids);
        for (Integer id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            String key="setmeal"+setmeal.getCategoryId();
            redisTemplate.delete(key);
        }
    }

    @Override
    public SetmealVO getAllById(Integer id) {
        //从setmeal中查
       SetmealVO setmealVo= setmealMapper.getAllById(id);
       //从setmealdish中查
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);
        setmealVo.setSetmealDishes(setmealDishList);
        return setmealVo;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SetmealDTO dto) {
        //修改setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);

        //删除setmealDish表中的对应数据
        Long setmealId= dto.getId();
        setmealDishMapper.removeBySetmeal(setmealId);

        //插入新的setmealdish表中的数据
        List<SetmealDish> setmealDishList = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.save(setmealDish);
        }
        String key = "setmeal:"+dto.getCategoryId();
        redisTemplate.delete(key);
    }

    @Override
    public void updateStatus(Integer status,Integer id) {
        setmealMapper.updateStatus(status,id);
        Setmeal setmeal = setmealMapper.getById(id);
        String key = "setmeal:"+setmeal.getCategoryId();
        redisTemplate.delete(key);
    }

    @Override
    public List<Setmeal> listBycategoryId(Integer categoryId) {
        String key = "setmeal:"+categoryId;
        String s = redisTemplate.opsForValue().get(key);
        if(!StringUtils.isEmpty(s)){
            List<Setmeal> setmeals = JSON.parseArray(s, Setmeal.class);
            return setmeals;
        }
        List<Setmeal> setmealList = setmealMapper.listBycategoryId(categoryId);
        redisTemplate.opsForValue().set(key,JSON.toJSONString(setmealList));
        return setmealList;
    }
}
